import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import pojo.Person;

import java.io.IOException;
import java.util.Date;

/**
 * @author lemon
 * @version V1.0
 * @date 2020/09/01
 */
public class Bulk {
    ObjectMapper mappers = new ObjectMapper();
    RestHighLevelClient client = ESClient.getClient();
    String index="person";
    String type="man";
    @Test
    public void bulkCreateDoc() throws IOException {
        //1.准备多个json数据
        Person p1 = new Person(1,"张三",23,new Date());
        Person p2 = new Person(2,"李四",24,new Date());
        Person p3 = new Person(3,"王五",25,new Date());

        String json1 = mappers.writeValueAsString(p1);
        String json2 = mappers.writeValueAsString(p2);
        String json3 = mappers.writeValueAsString(p3);

        //2.创建Request,将准备好的数据封装进去
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest(index, type, p1.getId().toString()).source(json1, XContentType.JSON));
        request.add(new IndexRequest(index, type, p2.getId().toString()).source(json2, XContentType.JSON));
        request.add(new IndexRequest(index, type, p3.getId().toString()).source(json3, XContentType.JSON));

        //3.用client执行
        BulkResponse resp = client.bulk(request, RequestOptions.DEFAULT);
        //4.输出结果
        System.out.println(resp.toString());

    }

    @Test
    public void bulkDeleteDoc(){
        //1.封装Request对象

    }
}
